package com.shangan.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.shangan.shortlink.admin.common.convention.result.Result;
import com.shangan.shortlink.admin.common.convention.result.Results;
import com.shangan.shortlink.admin.dto.req.UserLoginReqDTO;
import com.shangan.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.shangan.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.shangan.shortlink.admin.dto.resp.UserActualRespDTO;
import com.shangan.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.shangan.shortlink.admin.dto.resp.UserRespDTO;
import com.shangan.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Jerryzaj
 * @description TODO
 * @date: 2024/4/27 15:22
 */

/**
 * 根据用户名查询信息
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据username查询脱敏后的用户信息
     * @param username
     * @return UserRespDTO
     */
    @GetMapping("/api/short-link/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        return Results.success(userService.getUserByUsername(username));
    }

    /**
     * 根据usename查询未脱敏的用户信息
     * @param username
     * @return UserActualRespDTO
     */
    @GetMapping("/api/short-link/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username){
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }

    @GetMapping("/api/short-link/v1/hasUser")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 用户注册
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/v1/register/")
    public Result<Void> Register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    /**
     * 修改用户信息
     * @param userUpdateReqDto
     * @return
     */
    @PutMapping("/api/short-link/v1/user")
    public Result<Void> Update(@RequestBody UserUpdateReqDTO userUpdateReqDto){
        userService.update(userUpdateReqDto);
        return Results.success();
    }

    /**
     * 用户登录
     * @param userLoginReqDto
     * @return
     */
    @PostMapping("/api/short-link/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDto){
        return Results.success(userService.login(userLoginReqDto));
    }


    @GetMapping("/api/short-link/v1/user/checkLogin")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token){
        return Results.success(userService.checkLogin(username, token));
    }

    /**
     * 用户退出登录
     * @param username
     * @param token
     * @return
     */
    @DeleteMapping("/api/short-link/v1/user/checkLogOut")
    public Result<Void> checkLogOut(@RequestParam("username") String username, @RequestParam("token") String token){
        userService.logout(username, token);
        return Results.success();
    }
}
